var QQMapWX = require('utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
	key: 'SJKBZ-HOPW3-JPK3R-3LPZA-OE7VF-25FZS' //这里自己的key秘钥进行填充
});

// const app = getApp();
// const http = "/dpc/";
const http = process.env.NODE_ENV === "production" ? "http://apph5.hongsizg.com/apis" : "http://crmhongsi.com";
//const http = process.env.NODE_ENV === "production" ? "http://apph5.hongsizg.com/apis" : "http://api.reactphp.club";
// const http = "http://h5.reactphp.club/apis";
// const http = "http://apph5.hongsizg.com/apis";
//接口请求


const request = (item) => {

	if (item.showLoading) {
		uni.showLoading({
			title: '正在加载...',
			mask: true
		});
	}

	let header = {
		'Content-Type': 'application/x-www-form-urlencoded'
	};
	if (item.token) {
		let token = uni.getStorageSync("token");
		if (token) {
			// header.token = token;
			item.data.token = token;
		} else {
			uni.reLaunch({
				url: "/pages/login/login",
			});
			uni.showToast({
				icon: 'none',
				title: '请先登录'
			});
			uni.hideLoading();
			return false;
		}
	}


	uni.request({
		url: http + item.url,
		header: header,
		method: 'POST',
		data: item.data,
		dataType: 'json',
		sslVerify: false,
		success: function(data) {

			if (data.statusCode == 200) {
				uni.hideLoading();
				item.success(data.data);
				if (data.data.code == 300) {
					uni.reLaunch({
						url: "/pages/login/login",
					})
					uni.showToast({
						icon: 'none',
						title: '其他地方已登录'
					})
					uni.clearStorageSync();
				}
			} else {
				if (!item.hideShow && data.data.msg) {
					item.fail(data.data.code, data.data.msg);
				} else {
					item.fail(data.statusCode);
				}
				if (data.statusCode == 404) {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: data.statusCode,
						duration: 3500
					});
				}


			}

		},
		fail: function(e) {

			uni.hideLoading()
			uni.showToast({
				icon: 'none',
				title: '请求失败，请稍后重试sun',
				duration: 4500
			});
		}
	})
}

// 提示框
const showMsg = (title) => {
	uni.showToast({
		title: title,
		icon: 'none',
		duration: 2000
	});
}
// loading
const showLoading = (title) => {
	uni.showLoading({
		title: title
	})
}
// hideLoading
const hideLoading = () => {
	uni.hideLoading();
}


function getOpenAddress(address) {
	let that = this
	qqmapsdk.geocoder({
		address: address,
		complete: res => {
			if (res.status == 347) {
				uni.showToast({
					icon: "none",
					title: "查询无结果",
				})
				return
			}
			getMap(res.result.location, address);
		},
		fail: function() {
			uni.showToast({
				title: '无法定位到该地址',
				icon: "none"
			})
		}
	})
}

function getMap(lngLat, address) {
	uni.getLocation({
		type: 'gcj02', //返回可以用于uni.openLocation的经纬度
		success: function(res) {
			const latitude = res.latitude;
			const longitude = res.longitude;
			uni.openLocation({
				latitude: lngLat.lat,
				longitude: lngLat.lng,
				name: address,
				address: address,
				success: function(e) {}
			});
		},
		fail() {
			uni.showToast({
				icon: 'none',
				title: "获取当前定位失败,请确认是否打开定位!"
			})
		}
	});
}


//获取当前设备的tbar高度
function getStatusBarHeight() {
	let statusBarHeight = ''
	uni.getSystemInfo({
		success(e) {
			statusBarHeight = e.statusBarHeight + 46
		},
		fail(e) {}
	})
	return statusBarHeight
}





//时间戳转换时间
function formatTimeTwo(number, format) {
	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	var returnArr = [];
	var date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));
	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));
	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}

function newDate() {
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}

module.exports = {
	request: request,
	showMsg: showMsg,
	showLoading: showLoading,
	hideLoading: hideLoading,
	formatTimeTwo, //时间戳转换
	getOpenAddress, //地址跳转
	getStatusBarHeight, //获取设备状态栏高度
	http, //
	newDate
}
